package projet.volleystat;

import java.util.ArrayList;

import projet.volleystat.bdd.VolleyRepository;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class SelectTeamActivity extends Activity {
	protected ListView listv;
	protected VolleyRepository volleyDataBase;
	protected String order;
	protected ArrayList<Integer> teamsid;
	protected ArrayList<Team> teams;
	protected TeamAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_team);

		volleyDataBase = new VolleyRepository(getApplicationContext());
		volleyDataBase.open();

		Bundle bundle = getIntent().getExtras();
		teamsid = bundle.getIntegerArrayList("teamsidarray");

		teams = new ArrayList<Team>();

		for (int i = 0; i < teamsid.size(); i++) {
			try {
				teams.add(volleyDataBase.getTeamById(teamsid.get(i)));
			} catch (DatabaseException e) {
				e.printStackTrace();
			}
		}

		volleyDataBase.close();

		listv = (ListView) findViewById(R.id.selectteamlv);

		adapter = new TeamAdapter(getApplicationContext(),
				R.layout.listeitemlineteam, teams);

		listv.setAdapter(adapter);

		listv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent resIntent = new Intent();
				Team team = (Team) parent.getAdapter().getItem(position);
				resIntent.putExtra("selectedteamid", team.getId());
				setResult(RESULT_OK, resIntent);
				finish();
			}
		});
	}

	@Override
	public void onPause() {
		super.onPause();
		overridePendingTransition(0, 0);
		volleyDataBase.close();
	}
}